package com.lbb.springboot.auto.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lbb.springboot.auto.util.FormData.FieldData;

public class AutoGenerationMapper {

    static String tableName = "xcx_root_details";
//	static String className = DomysqlBean.toUpperCaseFirstOne(tableName
//			.substring(4, tableName.length()));
//	static String name = DomysqlBean.toUpperCaseFirstOne(DomysqlBean
//			.replaceLine(className));getMapper

    public static void main(String[] args) throws SQLException {
        // String className="Advert";
        List<FieldData> lf = new ArrayList<FieldData>();
        lf = DomysqlBean.autoFieldData(tableName);
        String mapper = getMapper(lf, tableName);

    }

    public static String getMapper(List<FieldData> lf, String tableName) {
        AutoGenerationMapper.tableName = tableName;
        String name = DomysqlBean.replaceLine(DomysqlBean.toUpperCaseFirstOne(tableName.substring(tableName.indexOf("_") + 1, tableName.length())));
        System.out.println("name>>>>>>>>>>>>>>>>{}" + name);
        String text = "";
        text += "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r";
        text += "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >\r";
        text += "<mapper namespace=\"com.lbb." + AutoGenerationSpringMVC2.project + ".mapper." + name + "Mapper\" >\r";
        text += "\t<resultMap id=\"BaseResultMap\" type=\"com.lbb." + AutoGenerationSpringMVC2.project + ".entity.dto."
                + name + "\" >\r";
        text += "\t\t<id column=\"id\" property=\"id\" jdbcType=\"INTEGER\" />\r";
        for (int i = 0; i < lf.size(); i++) {
            FieldData fieldData = lf.get(i);
            String datatype = fieldData.getDatatype();
            System.out.println(datatype);
            String jdbdType = null;
            if (datatype.contains("int") || datatype.contains("smallint")) {
                jdbdType = "INTEGER";
            }
            if (datatype.contains("varchar")) {
                jdbdType = "VARCHAR";
            }
            if (datatype.contains("double")) {
                jdbdType = "DOUBLE";
            }
            if (datatype.contains("datetime")) {
                jdbdType = "TIMESTAMP";
            }
            if (datatype.contains("text")) {
                jdbdType = "VARCHAR";
            }
            if (datatype.contains("decimal")) {
                jdbdType = "DECIMAL";
            }
            if (!fieldData.getField().equals("id")) {
                text += "\t\t<result column=\"" + fieldData.getField()
                        + "\" property=\"" + DomysqlBean.replaceLine(fieldData.getField())
                        + "\" jdbcType=\"" + jdbdType + "\" />\r";
            }
        }

        text += "\t</resultMap>\r";
        text += "</mapper>\r";
        System.out.println(text);
        saveFile(name, text, "mapping");
        return text;
    }

    public static void saveFile(String classname, String text, String pageName) {

        System.out.println("text:" + text);
        System.out.println("创建java类：" + classname);
        String filepath = Thread.currentThread().getContextClassLoader()
                .getResource(".").getPath();

        System.out.println("filepath:" + filepath);
        // LOG.out(filepath);

        String[] fileP = filepath.split("target");
        System.out.println(fileP[0]);
        File file = null;
//        if (!AutoGenerationSpringMVC2.project.contains("xuliyouka")) {
            file = new File(fileP[0] + "\\src\\main\\resources\\mapper\\"
                    + "/" + FormData.firstUpper(classname)

                    + "Mapper.xml");
//        } else {
//            file = new File(fileP[0] + "\\src\\main\\resources\\mapper\\xuliyouka\\"
//                    + "/" + FormData.firstUpper(classname)
//                    + "Mapper.xml");
//        }

        /*
         * File file = new File(
         * "F\:\lebaodai\myelicpseWs\trunk\src\main\java\cn\lebaodai\lbd\vo\Msg_push.java"
         * );
         */

        if (file.exists()) {
            file.delete();
        }

        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdir();
        }

        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedWriter writer;
        try {
            writer = new BufferedWriter(
                    new FileWriter(new File(file.getPath())));
            writer.write(text);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
